package cn.fintecher.gis.web;


import cn.fintecher.gis.entity.LogInfo;
import cn.fintecher.gis.entity.QLogInfo;
import cn.fintecher.gis.model.request.LogInfoRequest;
import cn.fintecher.gis.respository.LogInfoRepository;
import com.querydsl.core.BooleanBuilder;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/api/logInfo")
@Api(value = "操作日志管理", description = "操作日志管理")
public class LogInfoController {

    @Autowired
    private LogInfoRepository logInfoRepository;

    @GetMapping("/queryLogInfo")
    @ApiOperation(value = "查询日志管理信息", notes = "查询日志管理信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", dataType = "int", paramType = "query", value = "页数 (0..N)"),
            @ApiImplicitParam(name = "size", dataType = "int", paramType = "query", value = "每页大小."),
            @ApiImplicitParam(name = "sort", allowMultiple = true, dataType = "string", paramType = "query", value = "依据什么排序: 属性名(,asc|desc). ")
    })
    public ResponseEntity<Page<LogInfo>> queryRoleInfoList(LogInfoRequest request, Pageable pageable) {
        BooleanBuilder builder = new BooleanBuilder();
        QLogInfo qLogInfo = QLogInfo.logInfo;
        if (StringUtils.isNotEmpty(request.getUserName())) {
            builder.and(qLogInfo.userName.eq(request.getUserName()));
        }
        if (request.getEndTime() != null && request.getStartTime() != null) {
            builder.and(qLogInfo.recordTime.between(request.getStartTime(), request.getEndTime()));
        }
        Page<LogInfo> page = logInfoRepository.findAll(builder, pageable);
        return ResponseEntity.ok().body(page);
    }
}
